import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    add(state){
      state.count++
    },
    add2(state, stp){
      state.count += stp
    },
    sub(state){
      state.count--
    },
    subN(state, step){
      state.count -= step
    }
  },
  actions: {
    addSync(context){
      setTimeout( () => {
        context.commit('add')
      },1000)
    },
    addNSync(context, step){
      setTimeout( () => {
        context.commit('add2', step)
      },1000)
    },
    subAsync(context){
      setTimeout( () => {
        context.commit('sub')
      },1000)
    },
    subNAsync(context, step){
      setTimeout( () => {
        context.commit('subN', step)
      },1000)
    }
  },
  getters: {
    showNum(state){
      return '当前最新的数据【' + state.count + '】'
    }
  },
  modules: {
  }
})
